<script setup>
import { ref, defineEmits } from 'vue'
import { ElMessage } from 'element-plus'
import { addApi, updateApi } from '@/api/warehousingentry/warehousingEntry'

const emit = defineEmits(['refresh'])
const ruleFormRef = ref(null)
const rules = ref({
  batchNumber: [
    { required: true, message: '请输入批次号', trigger: 'blur' }
  ],
  consignorId: [
    { required: true, message: '请输入货主ID', trigger: 'blur' }
  ],
  totalCount: [
    { required: true, message: '请输入商品数量', trigger: 'blur' }
  ],
  predictedArrivalTime: [
    { required: true, message: '请选择预测到达时间', trigger: 'blur' }
  ],
  warehouseId: [
    { required: true, message: '请输入仓库ID', trigger: 'blur' }
  ]
})

const dialogVisible = ref(false)
const dialogFormData = ref({
  id: null,
  consignorId: null,
  batchNumber: '',
  totalCount: 0,
  predictedArrivalTime: '',
  warehouseId: null,
  damageCount: 0,
  surpassCount: 0,
  status: 1,
  lackCount: 0,
  saasId: null,
  isDel: 1
})

const props = defineProps({
  warehouseList: {
    default: () => []
  }
})
const resetForm = () => {
  dialogVisible.value = false
  dialogFormData.value = {
    id: null,
    consignorId: null,
    batchNumber: '',
    totalCount: 0,
    predictedArrivalTime: '',
    warehouseId: null,
    damageCount: 0,
    surpassCount: 0,
    status: 1,
    lackCount: 0,
    saasId: null,
    isDel: 1
  }
  ruleFormRef.value?.resetFields()
}

const confirm = async () => {
  if (!ruleFormRef.value) return
  await ruleFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      let res = null
      if (dialogFormData.value.id) {
        res = await updateApi(dialogFormData.value)
      } else {
        res = await addApi(dialogFormData.value)
      }
      if (res.code) {
        ElMessage.success('操作成功!')
        resetForm()
        emit('refresh')
      } else {
        ElMessage.error('操作失败！')
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}

defineExpose({
  dialogFormData,
  dialogVisible
})
</script>

<template>
  <el-dialog v-model="dialogVisible" title="入库单信息" width="600px">
    <el-form :model="dialogFormData" label-width="120px" ref="ruleFormRef" :rules="rules">
      <el-form-item label="批次号" prop="batchNumber">
        <el-input v-model="dialogFormData.batchNumber" placeholder="请输入批次号" />
      </el-form-item>

      <el-form-item label="货主ID" prop="consignorId">
        <el-input v-model="dialogFormData.consignorId" placeholder="请输入货主ID" />
      </el-form-item>

      <el-form-item label="商品数量" prop="totalCount">
        <el-input-number v-model="dialogFormData.totalCount" :min="0" />
      </el-form-item>

      <el-form-item label="预测到达时间" prop="predictedArrivalTime">
        <el-date-picker v-model="dialogFormData.predictedArrivalTime" type="datetime" placeholder="请选择预测到达时间"
          style="width: 100%" />
      </el-form-item>

      <el-form-item label="仓库" prop="warehouseId">
        <el-select v-model="dialogFormData.warehouseId" placeholder="请选择仓库">
          <el-option v-for="warehouse in warehouseList" :key="warehouse.id" :label="warehouse.name"
            :value="warehouse.id" />
        </el-select>
      </el-form-item>

      <el-form-item label="破损个数">
        <el-input-number v-model="dialogFormData.damageCount" :min="0" />
      </el-form-item>

      <el-form-item label="超出数量">
        <el-input-number v-model="dialogFormData.surpassCount" :min="0" />
      </el-form-item>

      <el-form-item label="缺少数量">
        <el-input-number v-model="dialogFormData.lackCount" :min="0" />
      </el-form-item>

      <el-form-item label="状态">
        <el-select v-model="dialogFormData.status" placeholder="请选择状态">
          <el-option label="未确认" :value="1" />
          <el-option label="部分已确认" :value="2" />
          <el-option label="全部确认" :value="3" />
          <el-option label="拣货中" :value="4" />
          <el-option label="已上架" :value="5" />
        </el-select>
      </el-form-item>

      <el-form-item label="是否有效">
        <el-switch v-model="dialogFormData.isDel" :active-value="1" :inactive-value="2" />
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="resetForm">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>